//
//  XMNavigationController.m
//  XMFriendCircleDemo
//
//  Created by chenming on 2018/1/26.
//  Copyright © 2018年 ChenXiaoMing. All rights reserved.
//

#import "XMNavigationController.h"


// Controller
#import "XMBaseViewController.h"

@interface XMNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation XMNavigationController

+ (void)initialize {
    // 设置为不透明
//    [[UINavigationBar appearance] setTranslucent:NO];
    // 设置导航栏背景颜色

    [[UINavigationBar appearance] setBarTintColor:[UIColor xm_colorWithHexString:@"ff69b4" alpha:1]];
    [[UINavigationBar appearance] setTintColor:[UIColor xm_colorWithHexString:@"333333" alpha:1]];


    // 设置导航栏标题文字颜色
    // 创建字典保存文字大小和颜色
    NSMutableDictionary * color = [NSMutableDictionary dictionary];
    color[NSFontAttributeName] = [UIFont systemFontOfSize:16];
    color[NSForegroundColorAttributeName] = [UIColor xm_colorWithHexString:@"333333" alpha:1];;
    [[UINavigationBar appearance] setTitleTextAttributes:color];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.navigationBarHidden = NO; //右滑返回可用
//    self.navigationBar.hidden = YES; // 隐藏导航栏
    
    id target = self.interactivePopGestureRecognizer.delegate;
    
    SEL action = NSSelectorFromString(@"handleNavigationTransition:");
    
    //设置返回手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:target action:action];
    
    pan.delegate = self;
    
    [self.view addGestureRecognizer:pan];
    
    self.interactivePopGestureRecognizer.enabled = NO;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}


- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.visibleViewController;
}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (self.childViewControllers.count >= 1) {
        return YES;
    } else {
        return NO;
    }
}

//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//        NSLog(@"%@", NSStringFromClass([touch.view class]));
    
    // 如果视图为UITableViewCellContentView（即点击tableViewCell），则不截获Touch事件
//    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
//        return NO;
//    }
//    return  YES;
//}

/** 重写push自定义返回按钮 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.childViewControllers.count > 0) {
//        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        [backButton setImage:[UIImage imageNamed:@"back_navi"] forState:UIControlStateNormal];
//        [backButton addTarget:self action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
//        //修改导航栏item
//        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
        //隐藏tarbbar
        viewController.hidesBottomBarWhenPushed = YES;
    }
    // 这个方法才是真正执行跳转
    [super pushViewController:viewController animated:animated];
    
    // 修改tabBra的frame
//    CGRect frame = self.tabBarController.tabBar.frame;
//    frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
//    self.tabBarController.tabBar.frame = frame;
}


// 重写popViewControllerAnimated,animation设置YES /** 解决tabbar早于返回显示 */
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
//    NSArray *temArray = self.navigationController.viewControllers;
//    for(UIViewController *temVC in temArray) {
//        if ([temVC isKindOfClass:[XMBaseViewController class]]) {
//            [self.navigationController popToViewController:temVC animated:YES];
//        }
//    }
    return [super popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
